package domain;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Scanner;


public class ProjectManagementSystem {

	/**
	 * @param args

	 * @throws IOException 

	 */

	public static void main(String[] args) throws IOException {

		int choice1;
		int choice2;
		int choice3;
		
		String projectName;
		String projectDesc;
		String projectStartDate;
	
		String activityName;
		String activityDesc;
		String activityStartDate;
		String activityDeliverable;
		
		String taskName;
    	String taskDesc;
		String taskStartDate;
		int taskHour;
    	String taskDeliverable;
    	
    	Project myPrjt = new Project();
    	Activity myAct = new Activity();
    	Task myTsk = new Task();
    	   	
		Scanner sc = new Scanner(System.in);
		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
   	
   	    
   	    do{	 
   	    	
		  System.out.println("\n------- Menu----------");
		  System.out.println("1. ADD NEW PROJECT");
		  System.out.println("2. REMOVE PROJECT");
		  System.out.println("3. UPDATE PROJECT");
		  System.out.println("4. DISPLAY PROJECT");
		  System.out.println("5. REPORT");
		  System.out.println("6. EXIT");
		  System.out.println("Enter choice [1,2,3,4,5,6]: ");
		  choice1 = sc.nextInt();
		  
		  switch (choice1)
	      {
	       
		  //case1...............................................................ADDING PROJECT................................................................
	        case 1: 
	        	 System.out.println("\n------- Adding Project----------");
	        	 System.out.println("Project Name: ");
	        	 projectName = reader.readLine();
	        	 System.out.println("Project Startdate(yyyy-MM-dd format): ");
	        	 projectStartDate = reader.readLine();
	        	 System.out.println("Project Description: ");
	        	 projectDesc = reader.readLine();
	        	 System.out.println("All Employees in the system");
	        	 Person myPerson = new Person();
	        	 myPerson.writeSelectAllEmployees();
	        	 System.out.println("Select Employee id that will be this Projects Manager:");
	        	 int secPM = sc.nextInt();
	        	 int projectManagerId = myPerson.selectPersonById(secPM).getId();
	        	 Project myProject = new Project(projectName, projectDesc, projectStartDate, projectManagerId);
	        	 
	        	 ArrayList<Activity> myActivityList = new ArrayList<Activity>();
	        	 ArrayList<Task> myTaskList = new ArrayList<Task>();
	        	 do{
	        		 System.out.println("1. ADD ACTIVITY");
	                 System.out.println("2. RETURN PREVIOUS MENU");
	        		 choice2 = sc.nextInt();		 
	        		 switch(choice2)
	        		 {
	        		 case 1:
	        			 System.out.println("\n------- Adding Activity----------");
	        			 System.out.println("Activity Name: "); 
	    	        	 activityName = reader.readLine();
	    	        	 System.out.println("Activity Description: "); 
	    	        	 activityDesc = reader.readLine();
	    	        	 System.out.println("Activity Startdate(yyyy-MM-dd format): ");
	    	        	 activityStartDate = reader.readLine();
	    	        	 System.out.println("Activity Deliverable: ");
	    	        	 activityDeliverable = reader.readLine();
	    	        	 Activity myActivity= new Activity(activityName, activityDesc, activityStartDate, null, activityDeliverable, null);
	    	        	 myActivity.setMyProject(myProject);
	    	        	 myActivityList.add(myActivity);
	        	     do{
	    	        	System.out.println("1. ADD TASK");
	    	        	System.out.println("2. RETURN PREVIOUS MENU");
	    	        	System.out.println("3. COMPLETE PROJECT CREATION");
	    	        	choice3 = sc.nextInt();
	    	        	switch(choice3)
	    	        	{
	    	        	case 1:
	    	            System.out.println("\n------- Adding Task ----------");   
	    	            Task myTask =  new Task();
	    	            myTask.setMyActivity(myActivity);
	    	            System.out.println("Task Name: ");
	    	            taskName = reader.readLine();
	    	            myTask.setName(taskName);
	    	            System.out.println("Task Description: ");
	    	            taskDesc = reader.readLine();
	    	            myTask.setDesc(taskDesc);
	    	            System.out.println("Task Startdate(yyyy-MM-dd format): ");
	    	            taskStartDate = reader.readLine();
	    	             myTask.setStartDate(taskStartDate);
	    	            System.out.println("Task Hours: ");
	    	        	taskHour = sc.nextInt();
	    	        	myTask.setHours(taskHour);
	    	        	System.out.println("Task Deliverable: ");
	    	        	taskDeliverable = reader.readLine();
	    	        	myTask.setDeliverable(taskDeliverable);
	    	        	myTask.setResourceId(1);
	    	        	myTaskList.add(myTask);
	    	        	break;
	    	        	case 2:
	    	        	break;
	    	        	case 3:
	    	        			
	    	        		if(myTaskList.size()>0)
	    	        		{
	    	        		for(Activity act : myActivityList)
	    		        	  {
	    		        		  for(Task tsk : myTaskList)
	    		        		  {
	    		        			 if(tsk.getMyActivity() == act)
	    		        			 {
	    		        				 if(myProject.create(myProject, act, tsk))
	    		        				 {
	    		        			     System.out.println("In project " + myProject.getName() +", " + tsk.getName() + " task is successfully added to the activity " + act.getName());
	    		        				 }
	    		        				 
	    		        				 }
	    		        		  }
	    		        	  }
	    	        		
	    		        	choice2=2;
	    	        		}
	    	        		else{
	    	        			System.out.println("Please add task to your activity");
	    	        			choice3=1;
	    	        		}   		
	    	  
	    	        	break;	
	    	        	default:
	  	       	          System.out.println("\nPlease choose a number between 1-3 only \n");
	    	        	}
	    	        	
	    	        }while(choice3==1);
	        		     
	        		
	        		break;//choice 2, case1 break
	        		case 2: //exit
	       	        break;
	        			//System.exit(0);
	       	        default:
	       	          System.out.println("\nPlease choose a number between 1-2 only \n");
                     }
	              } while(choice2==1);
	        	  
	        	 break;//choice 1, case 1 break
	      //case1..............................................................FINISH ADDING PROJECT..........................................................
	      //case2..............................................................REMOVING PROJECT...................................................................  
	        case 2:
	        	  System.out.println("\n------- Removing a Project ----------");
		          
		          if(myPrjt.selectAllProject()==null)
		          {
		        	  System.out.println("There are no projects in the system");
		        	  break;	
		          }
		          else{
		          System.out.println("Projects in the System:");
		          myPrjt.writeAllProject();
		          System.out.println("Please enter the project id that you want to remove"); 
		          int choice4 = sc.nextInt();
		          System.out.println("The project "+ myPrjt.selectById(choice4).getName() + " will be removed");
		          myPrjt.removeProject(myPrjt.selectById(choice4));
			      System.out.println("The project removed successfully");
		          }
	          break;
	      //case2...................................................................FINISH REMOVING PROJECT................................................................
	      //case3...................................................................UPDATING PROJECT....................................................................
	        case 3:
	        	
	        	System.out.println("\n------- Updating a Project ----------");
		          if(myPrjt.selectAllProject()==null)
		          {
		        	  System.out.println("There are no projects in the system");
		        	  break;	
		          }
		          else{
		          System.out.println("Projects in the System:");
			        
		          myPrjt.writeAllProject();
	          
		            System.out.println("Please enter the project id that you want to update");
		            int choice5 = sc.nextInt();
		            Project updatingProject = myPrjt.selectById(choice5);
		            System.out.println("The project "+updatingProject.getName() + " you want to update");
		            int choice6;
		            
		            do{
		            	System.out.println("Enter choice");
		            	System.out.println("1. UPDATE PROJECT INFORMATION");
		            	System.out.println("2. UPDATE PROJECTS ACTIVITIES");
		            	System.out.println("3. COMPLETE UPDATE");
		            	System.out.println("4. ADD ACTIVITY TO PROJECT");
		                choice6 = sc.nextInt();
		            	
		                switch(choice6)
		            	{
		            	case 1:
		            	
		            	int choice9;
		            	  do{
		            		
		            		System.out.println("1. UPDATING PROJECT NAME " + updatingProject.getName());
		            		System.out.println("2. UPDATING PROJECT DESCRIPTION " + updatingProject.getDesc());
			            	System.out.println("3. UPDATING PROJECT STARTDATE " + updatingProject.getDateToString());
			            	System.out.println("4. COMPLETE UPDATE");
			            	System.out.println("Please select choice that you want to update");
			            	choice9 = sc.nextInt();
		            		  switch(choice9){
		            		 
		            		  case 1:
		            			  System.out.println("ENTER THE PROJECT NAME : ");
		            			  String updatingPName = reader.readLine();
		            			  updatingProject.setName(updatingPName);
		            			  break;
		            		  case 2:
		            			  System.out.println("ENTER THE PROJECT NEW DESCRIPTION : ");
		            			  String updatingPDescription = reader.readLine();
		            			  updatingProject.setDesc(updatingPDescription);
		            			  break;
		            		  case 3:
		            			  System.out.println("ENTER THE PROJECT NEW STARTDATE : ");
		            			  String updatingPStartDate = reader.readLine();
		            			  updatingProject.setStartDate(updatingPStartDate);
		            			  break;
		            		  case 4:
		            			  updatingProject.updateProject(updatingProject);
		            			  System.out.println("Project information is updated successfully ");
		            			  break;
		            		  }
		            	  }while(choice9!=4);
		            	break;
		            	
		            	
		            	case 2:
		            	
		            		int choice8;
		            		if(myAct.selectbyProject(updatingProject)!=null)
		            		{
		            		System.out.println("ACTIVITIES IN THE "+updatingProject.getName() +" PROJECT :");
			            	
			            	myAct.writeSelectByProject(updatingProject);
			            	System.out.println("Please enter the activity id that you want to update");
			            	int choice7 = sc.nextInt();
			            	Activity updatingActivity = myAct.selectById(choice7);
			            	
		            			do{
		              
		            	
		            	System.out.println("1. UPDATE ACTIVITY NAME: "+updatingActivity.getName());
		            	System.out.println("2. UPDATE ACTIVITY DESCRIPTION: "+updatingActivity.getDesc());
		            	System.out.println("3. UPDATE ACTIVITY STARTDATE: "+updatingActivity.getDateToString());
		            	System.out.println("4. UPDATE ACTIVITY DELIVERABLE: "+updatingActivity.getDeliverable());
		            	System.out.println("5. UPDATE ACTIVITY TASKS");
		            	System.out.println("6. REMOVING ACTIVITY");
		            	System.out.println("7. COMPLETE ACTIVITY UPDATE");
		            	System.out.println("8. ADD TASK TO ACTIVITY");
			            System.out.println("Please select choice that you want to update");
		            	choice8 = sc.nextInt();
			            String updatingDel;
			            String updatingStartDate;
		            	 switch(choice8)
			              {
		            	 case 1:
		            		 System.out.println("ENTER THE NEW ACTIVITY NAME: ");
		            		 String updatingName = reader.readLine();
		            		 updatingActivity.setName(updatingName);
		            		 break;
		            	 case 2:
		            		 System.out.println("ENTER THE NEW ACTIVITY DESCRIPTION: ");
		            		 String updatingDesc = reader.readLine();
		            		 updatingActivity.setDesc(updatingDesc);
		            		 break;
		            	 case 3:
		            		 System.out.println("ENTER THE NEW ACTIVITY STARTDATE: ");
		            		 updatingStartDate = reader.readLine();
		            		 updatingActivity.setStartDate(updatingStartDate);
		            		 break;
		            	 case 4: 
		            		 System.out.println("ENTER THE NEW DELIVERABLE: ");
		            		 updatingDel = reader.readLine();
		            		 updatingActivity.setDeliverable(updatingDel);
		            		 break;
		            	
		            	 case 5:
		            		 int choice11;
		            		 
		            		 if(myTsk.selectbyActivity(updatingActivity)!=null)
		            		 {
		            			    myTsk.writeSelectByActivity(updatingActivity);
						            System.out.println("Please enter the task id that you want to update");
						            int choice10 = sc.nextInt();
						            Task updatingTask = myTsk.selectById(choice10);
		            			 do{
		            			        
		            				System.out.println("1. UPDATING TASK NAME: "+updatingTask.getName());
					                System.out.println("2. UPDATING TASK DESCRIPTION: "+updatingTask.getDesc());
					                System.out.println("3. UPDATING TASK STARTDATE: "+updatingTask.getDateToString());
					                System.out.println("4. UPDATING TASK HOURS: "+updatingTask.getHours());
					            	System.out.println("5. UPDATING TASK DELIVERABLE: "+updatingTask.getDeliverable());
					            	System.out.println("6. UPDATING TASK RESOURCEID :"+updatingTask.getResourceId() );
					            	System.out.println("7. REMOVING TASK");
					            	System.out.println("8. COMPLETE TASK UPDATE");
						            System.out.println("Please select choice that you want to update");
					            	choice11 = sc.nextInt();
					            	switch(choice11)
					            	{
					            	case 1:
					            		 System.out.println("ENTER THE NEW TASK NAME: ");
					            		 String updatingTName = reader.readLine();
					            		 updatingTask.setName(updatingTName);
					            		break;         	
					            	case 2:
					            		 System.out.println("ENTER THE NEW TASK DESCRIPTION: ");
					            		 String updatingTDesc = reader.readLine();
					            		 updatingTask.setDesc(updatingTDesc);
					            		break;
					            	case 3:
					            		 System.out.println("ENTER THE NEW TASK STARTDATE: ");
					            		 String updatingTStartDate = reader.readLine();
					            		 updatingTask.setStartDate(updatingTStartDate);
					            		break;
					            	case 4:
					            		 System.out.println("ENTER THE NEW TASK HOURS: ");
					            		 Integer updatingTHours =  sc.nextInt();
					            		 updatingTask.setHours(updatingTHours); 
					            		break;
					            	case 5:
					            		 System.out.println("ENTER THE NEW TASK DELIVERABLE: ");
					            		 String updatingTDel = reader.readLine();
					            		 updatingTask.setDeliverable(updatingTDel);
					            		 break;
					            	case 6:
					            		 System.out.println("ENTER THE NEW TASK RESOURCEID: ");
					            		 int updatingTResourceId = sc.nextInt();
					            		 updatingTask.setResourceId(updatingTResourceId);
					            		break;	
					            	case 7:
					            		updatingTask.removeTask(updatingTask);
					            	    System.out.println("The task is removed successfully");
					            		break;
					            	case 8:
					            		updatingTask.updateTask(updatingTask);
					            		System.out.println("Task is successfully updated");
					            		break;
					            					
					            	}
					            }while(choice11!=7 && choice11!=8);
		            		 }
		            		 else{
		            			 
		            			 System.out.println("There is no more activity inside the project");
		            		 }
		            		 break;
		            	 case 6:
		           	
		            		 updatingActivity.removeActivity(updatingActivity);
		            		 System.out.println("The activity is removed successfully");
		        		     break;  
		            	
		            	 
		            	 case 7:
		            		 updatingActivity.updateActivity(updatingActivity);
		            		 System.out.println("Activity is successfully updated");
		            		 break;
			            
		            	 case 8:
		            		    System.out.println("\n------- Adding Task ----------");   
			    	            Task myTask =  new Task();
			    	            myTask.setMyActivity(updatingActivity);
			    	            System.out.println("Task Name: ");
			    	            taskName = reader.readLine();
			    	            myTask.setName(taskName);
			    	            System.out.println("Task Description: ");
			    	            taskDesc = reader.readLine();
			    	            myTask.setDesc(taskDesc);
			    	            System.out.println("Task Startdate(yyyy-MM-dd format): ");
			    	            taskStartDate = reader.readLine();
			    	             myTask.setStartDate(taskStartDate);
			    	            System.out.println("Task Hours: ");
			    	        	taskHour = sc.nextInt();
			    	        	myTask.setHours(taskHour);
			    	        	System.out.println("Task Deliverable: ");
			    	        	taskDeliverable = reader.readLine();
			    	        	myTask.setDeliverable(taskDeliverable);
			    	        	myTask.setResourceId(1);
			    	        	myTask.setActivityId(updatingActivity.getId());
			    	            myTask.addTask(myTask);
		            		 break;
			              
			              }
		            			
		            	 
		            	}while(choice8!=6 && choice8!=7);
		            		}
		            		else{
		            			System.out.println("There is no more activity inside the project");
		            		}
		            	
		            	break;
		            	
		            	
		            	case 3:	
		            		System.out.println("Project is updated successfuly");
		            	break;
		            	
		            	case 4:
            ArrayList<Activity> myLastActivityList = new ArrayList<Activity>();
       	    ArrayList<Task> myLastTaskList = new ArrayList<Task>();
       	    int secim ;
       	    do{
       		    System.out.println("1. ADD ACTIVITY");
                System.out.println("2. RETURN PREVIOUS MENU");
                secim  = sc.nextInt();	 
       		 switch(secim)
       		 {
       		 case 1:
       			 System.out.println("\n------- Adding Activity----------");
       			 System.out.println("Activity Name: "); 
   	        	 activityName = reader.readLine();
   	        	 System.out.println("Activity Description: "); 
   	        	 activityDesc = reader.readLine();
   	        	 System.out.println("Activity Startdate(yyyy-MM-dd format): ");
   	        	 activityStartDate = reader.readLine();
   	        	 System.out.println("Activity Deliverable: ");
   	        	 activityDeliverable = reader.readLine();
   	        	 Activity myActivity= new Activity(activityName, activityDesc, activityStartDate, null, activityDeliverable, null);
   	        	 myActivity.setMyProject(updatingProject);
   	        	 myLastActivityList.add(myActivity);
   	        	int secim2;
   	        	 do{
   	        	System.out.println("1. ADD TASK");
   	        	System.out.println("2. RETURN PREVIOUS MENU");
   	        	System.out.println("3. COMPLETE ACTIVITY CREATION");
   	        	secim2 = sc.nextInt();
   	        	switch(secim2)
   	        	{
   	        	case 1:
   	            System.out.println("\n------- Adding Task ----------");   
   	            Task myTask =  new Task();
   	            myTask.setMyActivity(myActivity);
   	            System.out.println("Task Name: ");
   	            taskName = reader.readLine();
   	            myTask.setName(taskName);
   	            System.out.println("Task Description: ");
   	            taskDesc = reader.readLine();
   	            myTask.setDesc(taskDesc);
   	            System.out.println("Task Startdate(yyyy-MM-dd format): ");
   	            taskStartDate = reader.readLine();
   	             myTask.setStartDate(taskStartDate);
   	            System.out.println("Task Hours: ");
   	        	taskHour = sc.nextInt();
   	        	myTask.setHours(taskHour);
   	        	System.out.println("Task Deliverable: ");
   	        	taskDeliverable = reader.readLine();
   	        	myTask.setDeliverable(taskDeliverable);
   	        	myTask.setResourceId(1);
   	        	myLastTaskList.add(myTask);
   	        	break;
   	        	case 2:
   	        	break;
   	        	case 3:
   	        			
   	        		if(myLastTaskList.size()>0)
   	        		{
   	        		for(Activity act : myLastActivityList)
   		        	  {
   		        		  for(Task tsk : myLastTaskList)
   		        		  {
   		        			 if(tsk.getMyActivity() == act)
   		        			 {
   		        				 if(updatingProject.create(updatingProject, act, tsk))
   		        				 {
   		        			     System.out.println("In project " + updatingProject.getName() +", " + tsk.getName() + " task is successfully added to the activity " + act.getName());
   		        				 }
   		        				 
   		        				 }
   		        		  }
   		        	  }
   	        		
   	        		secim2=2;
   	        		}
   	        		else{
   	        			System.out.println("Please add task to your activity");
   	        			choice3=1;
   	        		}   		
   	  
   	        	break;	
   	        	default:
 	       	          System.out.println("\nPlease choose a number between 1-3 only \n");
   	        	}
   	        	
   	        }while(secim2==1);
       		     
       		
       		break;//choice 2, case1 break
       		case 2: //exit
      	        break;
       			//System.exit(0);
      	        default:
      	          System.out.println("\nPlease choose a number between 1-2 only \n");
                }
             } while(secim==1);
		            		break;
		            	
		            	}
		            
		            }while(choice6!=3);
		          }
		            break;
		            
		    //case3.................................................................FINISH UPDATING PROJECT..................................................................        
	            case 4:
	        	//DISPLAY PROJECT
	        	 System.out.println("\n------- Displaying a Project ----------");
		          Project myPro = new Project();
		          if(myPro.selectAllProject()==null)
		          {
		        	  System.out.println("There are no projects in the system");
		        	  break;	
		          }

		          else{
		          System.out.println("Projects in the System:");
		          myPro.writeAllProject();
		          System.out.println("Please enter the project ID that you want to display"); 
		          int choice = sc.nextInt();
		          myPro = myPro.selectById(choice); 
		          Display disp = new Display();
		          disp.generateCsvFile(myPro);
		          disp.printCsv(myPro);
		          System.out.println("The csv file is created.It is in the path c:\\displayproject.csv");
		          
		          }
	        	break;
	        case 5:
	        	//REPORT
	        	Report myReport = new Report();
	        	myReport.generateExcelFile();
	        	System.out.println("The report is created in the path c:\\report.xls");
	          break;
	        case 6: //exit
	          System.exit(0);
	        default:
	          System.out.println("\nPlease choose a number between 1-4 only \n");  		 
	       		
	      }
	
	}while(choice1!=6);
		 

	}
}
